package com.guoqi.easyvideo;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MockUtils {

    private static String getImgUrl() {
        List<String> imgs = new ArrayList<>();
        imgs.add("http://img4.duitang.com/uploads/item/201408/19/20140819172216_zPzZT.jpeg");
        imgs.add("http://img2.niutuku.com/desk/221/221-25356.jpg");
        imgs.add("http://img18.poco.cn/mypoco/myphoto/20160606/17/179255386201606061741421503547600541_018.jpg");
        imgs.add("http://img4.duitang.com/uploads/item/201404/16/20140416232354_VFTyY.jpeg");
        imgs.add("http://img3.duitang.com/uploads/item/201506/20/20150620224057_Ua4uL.jpeg");
        imgs.add("http://s.image.hnol.net/x/900x0/auto/http://image.hnol.net/c/2011-11/08/16/201111081649429421-1988454.jpg");
        imgs.add("http://img4.duitang.com/uploads/item/201502/25/20150225134028_XW3WC.thumb.700_0.jpeg");
        imgs.add("http://imgsrc.baidu.com/forum/pic/item/d1160924ab18972bb10a4e83e6cd7b899e510a42.jpg");
        imgs.add("http://img5q.duitang.com/uploads/item/201501/23/20150123231841_wCENF.jpeg");
        imgs.add("http://img4.duitang.com/uploads/item/201511/06/20151106141943_4tPrv.jpeg");
        return imgs.get(new Random().nextInt(imgs.size()));
    }

    private static String getVideoUrl() {
        List<String> imgs = new ArrayList<>();
        imgs.add("http://113.142.51.22/om.tc.qq.com/AkexgXP-kqHi8vleiodTqxZ6QN3Ows-HIPsi5i7M7iPs/uwMROfz2r57IIaQXGdGnC2ddDmZUA-P9pUvDyp7Vcj06VB05/shg_24793402_50001_28f63cd864ae3eb9bede5e8667f17be8.f622.mp4?vkey=47E4DC4DE77D1303F7CE46282838ABF79D5D9D949C3AC5D61A0EFE07A1F394204241D1BD7E952FE110B3312B36F02A108FCE5F8C5D54F667B1DBCFED42EDC3C71DDD0E50C93FB220527AB19D7AC87C22DB797AB7F3E84796C2C60CFFAC3524ADBD896E7CEBD4642E3C74C94BA8E54E3D");
        imgs.add("http://59.49.45.18/vkp.tc.qq.com/A5RmCPmbR12x9L_sSEJavGCQN89b1_1g3TRwTIHKJhHE/uwMROfz2r5zIIaQXGdGnC2dfiMUeVB-s2C0-myA3-1969TVZ/t0031z0pv6p.m1.mp4?vkey=E6ABD1DA118B3DF5211D368169962664E61B14E269D45F63F26DCA9B42D89A2437903EB0DF172833825476926D2EAED8A498EEBEEF81D277723F140FC368BDAD495DB21C6FDD66B3223AFF8EFF6B98B4A2CDF46DDE59D2D3C3311CFF7E90514098F781EE1D14889636D987C7946874F1");
        imgs.add("http://ugcsjy.qq.com/uwMROfz2r5zEIaQXGdGnC2dfDmafRkP9ujxgqKjuATzMrE-2/i0898jhwvmj.mp4?vkey=14725C73827D60AAAF327B0C782C39B4EF5C1C98D8214C2FBB6118133177DBB7016AC55F6C7DD1893543F96AA81B9A90992DE01D29937461D7B9E0D2F5C3B1DFA990B68DD630EAB3B457DE6400709621BE937C1823BF02F3D8FBA11A87C1FADFFDFD663E347C287DA2573B323A5309CA");
        return imgs.get(new Random().nextInt(imgs.size()));
    }

    public static <T> T mockData(Class<T> clazz) {
        T t = null;
        try {
            t = clazz.newInstance();
            setValue(t, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> List<T> mockList(Class<T> clazz) {
        return mockList(clazz, 10);
    }

    public static <T> List<T> mockList(Class<T> clazz, int count) {
        List<T> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            T t = mockData(clazz);
            if (t != null) {
                list.add(t);
            }
        }
        return list;
    }

    private static <T> void setValue(Object object, Class<T> clazz) throws IllegalAccessException {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (Modifier.isFinal(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) {
                continue;
            }

            Object value = getFieldMockValue(field);
            if (value != null) {
                field.setAccessible(true);
                field.set(object, value);
            }
        }
    }

    /**
     * 模拟基础类型数据
     */
    private static Object getFieldMockValue(Field field) {
        Class clazzType = field.getType();
        Object value = null;
        if (clazzType == int.class || clazzType == Integer.class) {
            value = new Random().nextInt(100);
        } else if (clazzType == long.class || clazzType == Long.class) {
            value = Math.abs(new Random().nextLong() % 10000);
        } else if (clazzType == float.class || clazzType == Float.class) {
            value = new Random().nextFloat();
        } else if (clazzType == double.class || clazzType == Double.class) {
            value = new Random().nextDouble();
        } else if (clazzType == String.class) {
            String name = field.getName();
            // TODO: 可添加修改更多规则
            if (name.toLowerCase().contains("img") || name.toLowerCase().contains("url")) {
                //value = getImgUrl();
                value = getVideoUrl();
            } else if (name.toLowerCase().contains("video")) {
                value = getVideoUrl();
            } else {
                value = name + "_" + new Random().nextInt(100);
            }
        } else if (clazzType == boolean.class || clazzType == Boolean.class) {
            value = new Random().nextBoolean();
        }
        return value;
    }

}
